$(".gou-jixu").click(function () {
  window.location.href = "leibiao.html";
});

//第san次提交
$(".gou-denglu").click(function () {
  if (sessionStorage.getItem("vipName") == null) {
    window.location.href = "denglu.html";
  }
});
$(".gou-zhuce").click(function () {
  window.location.href = "zhuce.html";
});

if (sessionStorage.getItem("vipName") != null) {
  $(".gou-denglu").html("已登录");
}

$.get(
  "goodsAndShoppingCart/getShoppingCart.php",
  {
    vipName: sessionStorage.getItem("vipName"),
  },
  function (data) {
    // for(let i=0;i<data.len)
    let json = JSON.parse(data);
    console.log(json);
    let a = json.length;
    for (let i = 0; i < a; i++) {
      $(".gou-shangpinlie").append(
        '<tr><td class="gou-xuhao">1</td><td class="gou-shangpin"><img src="img/1/da1.png" alt=""><div>DJI Mavic 3</div></td><td><span class="gou-jiage"style="text-align:center; margin:auto;display:block">111111</span></td><td><div class="jiajian"><input class="gou-jia" type="button" value="+" style="width: 30px;"><input type="text" value="1" class="gou-shuliang"><input class="gou-jian" type="button" value="-" style="width: 30px;" ></div></td><td><span class="gou-xiaoji" style="text-align:center; margin:auto;display:block">0</span></td><td><div class="shanchu"><img src="img/shanchu.png" alt="" ></div></td></tr>'
      );

      $(".gou-xuhao").eq(i).html(i);

      $(".gou-shangpin>img")
        .eq(i)
        .attr("src", `img/liebiao/${json[i]["goodsImg"]}`);
      $(".gou-shangpin>div").eq(i).html(`${json[i]["goodsName"]}`);

      $(".gou-jiage").eq(i).html(`￥${json[i]["goodsPrice"]}`);
      console.log(`${json[i]["goodsCount"]}`);
      $(".gou-shuliang").eq(i).val(`${json[i]["goodsCount"]}`);

      $(".gou-xiaoji")
        .eq(i)
        .html(`${json[i]["goodsPrice"]}` * `${json[i]["goodsCount"]}`);
    }
    let index = 0;
    for (let j = 0; j < a; j++) {
      index += `${json[j]["goodsPrice"]}` * `${json[j]["goodsCount"]}`;
      $(".hejishu").html(index);
    }

    for (let i = 0; i < a; i++) {
      //删除
      $(".shanchu")
        .eq(i)
        .click(function () {
          $.get(
            "goodsAndShoppingCart/deleteGoods.php",
            {
              vipName: sessionStorage.getItem("vipName"),
              goodsId: `${json[i]["goodsId"]}`,
            },
            function (data) {
              if (data == 1) {
                console.log("yes");
                location.reload(true);
              } else {
                console.log("no");
              }
            }
          );
        });

      //加
      let t = $(".gou-shuliang").eq(i).val() / 1;
      $(".gou-jia")
        .eq(i)
        .click(function () {
          let t = $(".gou-shuliang").eq(i).val() / 1;
          t += 1;
          $(".gou-shuliang").eq(i).val(t);
          $(".gou-xiaoji").eq(i).html(`${json[i]["goodsPrice"]}` * t);

          let index = 0;
          for (let j = 0; j < a; j++) {
           index += $(".gou-xiaoji").eq(j).html()/1;
            $(".hejishu").html(index);
           

            $.get(
              "goodsAndShoppingCart/updateGoodsCount.php",
              {
                vipName: sessionStorage.getItem("vipName"),
                goodsId: `${json[i]["goodsId"]}`,
                goodsCount: t
              },function(data){
                if(data==1){
                  console.log("yes")
                }else{
                  console.log("no")
                }
              }
  
            )
          }

        });

      //减
      // let p=$(".gou-shuliang").eq(i).val()/1
      $(".gou-jian")
        .eq(i)
        .click(function () {
          let p = $(".gou-shuliang").eq(i).val() / 1;
          if (p == 1) {
            $(".gou-shuliang").eq(i).val(1);
          } else {
            p -= 1;
            $(".gou-shuliang").eq(i).val(p);
            $(".gou-xiaoji").eq(i).html(`${json[i]["goodsPrice"]}` * p);

            let index = 0;
            for (let j = 0; j < a; j++) {
             index += $(".gou-xiaoji").eq(j).html()/1;
              $(".hejishu").html(index);
              

              $.get(
                "goodsAndShoppingCart/updateGoodsCount.php",
                {
                  vipName: sessionStorage.getItem("vipName"),
                  goodsId: `${json[i]["goodsId"]}`,
                  goodsCount: p
                },function(data){
                  if(data==1){
                    console.log("yes")
                  }else{
                    console.log("no")
                  }
                }
    
              )
            }
          }
        });
    }
  }
);


// function jiajian(i){
//           $.get(
//             "goodsAndShoppingCart/updateGoodsCount.php",
//             {
//               vipName: sessionStorage.getItem("vipName"),
//               goodsId: `${json[i]["goodsId"]}`,
//               goodsCount: index
//             },function(data){
//               if(data==1){
//                 console.log("yes")
//               }else{
//                 console.log("no")
//               }
//             }

//           )}

// for(let i=0;i<)
// $(".shanchu").click(function(){
//   $.get("goodsAndShoppingCart/deleteGoods.php",
// {
// "vipName":sessionStorage.getItem('vipName'),
// "goodsId":
// },
//   )

// })
